package com.group4.Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.group4.DAO.ForumDAO;
import com.group4.DAO.PostDAO;
import com.group4.DAO.UserDAO;
import com.group4.DB.Forum;
import com.group4.DB.Post;
import com.group4.DB.User;

@WebServlet( name="AddPost", displayName="AddPost", urlPatterns = {"/post/add"})
public class AddPost extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6331948085594599031L;
	

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");
		// Get the printwriter object from response to write the required json
		// object to the output stream

		String topic = request.getParameter("topic");
		String text = request.getParameter("entry");
		String userName = request.getParameter("username");
		String videoId = request.getParameter("videoId");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();

		if (topic == null || text == null || userName == null) {
			jsonObject.addProperty("error", "missing parameter!");
			out.print(jsonObject);
			out.flush();
		} else {
			UserDAO user = User.getUserWithUsername(userName);
			if (user != null) {
				ForumDAO forum = Forum.getForum(topic);
				if (forum != null) {
					PostDAO post = new PostDAO();
					java.util.Date utilDate = new java.util.Date();
					long time = utilDate.getTime();
					post.setPostDateTime(new Date(time));
					post.setPostForumId(forum.getID());
					post.setPostOwner(user.getID());
					post.setPostText(text);
					post.setPostTopic(topic);
					if(videoId != null){
						post.setPostVideoId(Integer.valueOf(videoId));
					}
					

					boolean res = Post.addPost(post);
					if (res) {
						Gson gson = new Gson();
						String json = gson.toJson(post);
						out.print(json);
						out.flush();
					} else {
						jsonObject.addProperty("error",
								"Error occured while adding post!");
						out.print(jsonObject);
						out.flush();
					}
				} else {
					forum = new ForumDAO();
					java.util.Date utilDate = new java.util.Date();
					long time = utilDate.getTime();
					forum.setForumDateTime(new Date(time));
					forum.setForumHeadText(topic);
					forum.setForumOwnerId(user.getID());
					boolean res = Forum.addForum(forum);

					if (res) {
						forum = Forum.getForum(topic);
						PostDAO post = new PostDAO();
						utilDate = new java.util.Date();
						time = utilDate.getTime();
						post.setPostDateTime(new Date(time));
						post.setPostForumId(forum.getID());
						post.setPostOwner(user.getID());
						post.setPostText(text);
						post.setPostTopic(topic);
						if(videoId != null)
							post.setPostVideoId(Integer.valueOf(videoId));

						res = Post.addPost(post);
						if (res) {
							Gson gson = new Gson();
							String json = gson.toJson(post);
							out.print(json);
							out.flush();
						} else {
							jsonObject.addProperty("error",
									"Error occured while adding post!");
							out.print(jsonObject);
							out.flush();
						}
					} else {
						jsonObject.addProperty("error",
								"Error occured while adding topic!");
						out.print(jsonObject);
						out.flush();
					}
				}

			} else {
				jsonObject.addProperty("error", "No such user");
				out.print(jsonObject);
				out.flush();
			}
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String topic = request.getParameter("topic");
		String text = request.getParameter("entry");
		String userName = request.getParameter("username");
		String videoId = request.getParameter("videoId");

		PrintWriter out = response.getWriter();
		JsonObject jsonObject = new JsonObject();

		if (topic == null || text == null || userName == null) {
			jsonObject.addProperty("error", "missing parameter!");
			out.print(jsonObject);
			out.flush();
		} else {
			UserDAO user = User.getUserWithUsername(userName);
			if (user != null) {
				ForumDAO forum = Forum.getForum(topic);
				if (forum != null) {
					PostDAO post = new PostDAO();
					java.util.Date utilDate = new java.util.Date();
					long time = utilDate.getTime();
					post.setPostDateTime(new Date(time));
					post.setPostForumId(forum.getID());
					post.setPostOwner(user.getID());
					post.setPostText(text);
					post.setPostTopic(topic);
					post.setPostVideoId(Integer.valueOf(videoId));

					boolean res = Post.addPost(post);
					if (res) {
						Gson gson = new Gson();
						String json = gson.toJson(post);
						out.print(json);
						out.flush();
					} else {
						jsonObject.addProperty("error",
								"Error occured while adding post!");
						out.print(jsonObject);
						out.flush();
					}
				} else {
					forum = new ForumDAO();
					java.util.Date utilDate = new java.util.Date();
					long time = utilDate.getTime();
					forum.setForumDateTime(new Date(time));
					forum.setForumHeadText(topic);
					forum.setForumOwnerId(user.getID());
					boolean res = Forum.addForum(forum);

					if (res) {
						forum = Forum.getForum(topic);
						PostDAO post = new PostDAO();
						utilDate = new java.util.Date();
						time = utilDate.getTime();
						post.setPostDateTime(new Date(time));
						post.setPostForumId(forum.getID());
						post.setPostOwner(user.getID());
						post.setPostText(text);
						post.setPostTopic(topic);
						post.setPostVideoId(Integer.valueOf(videoId));

						res = Post.addPost(post);
						if (res) {
							Gson gson = new Gson();
							String json = gson.toJson(post);
							out.print(json);
							out.flush();
						} else {
							jsonObject.addProperty("error",
									"Error occured while adding post!");
							out.print(jsonObject);
							out.flush();
						}
					} else {
						jsonObject.addProperty("error",
								"Error occured while adding topic!");
						out.print(jsonObject);
						out.flush();
					}
				}

			} else {
				jsonObject.addProperty("error", "No such user");
				out.print(jsonObject);
				out.flush();
			}
		}
	}

}
